package com.forotuxpan.appforotuxpan;

import android.app.ActionBar;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
//import android.support.v7.app.ActionBarActivity;
import android.app.Activity;

/**
 * Created by gmartinezh on 20/08/2014.
 */
public class BaseActionBarActivity extends Activity {
    //public static final String nombreBD = "bs";
    //public static final String tblSettingLanguage = "SettingLanguage";
    //public static final String createtblSettingLanguage = "create table if not exists "
    //        + tblSettingLanguage + " (IdLanguage int, Language text);";
    //public SQLiteDatabase db;
    ProgressDialog progressDialog = null;
    public AsyncTask<Void, Void, Void> task = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    public void hiddenLoading() {
        progressDialog.dismiss();
    }

    public void showLoading(Context context) {
        progressDialog = new ProgressDialog(context);
        progressDialog.show();
        progressDialog.setContentView(R.layout.custom_progressdialog);
        progressDialog.setCancelable(true);//se ppdrá cerrar simplemente pulsando back
        progressDialog.setCancelable(true);
        progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                if (task != null) {
                    if (task.getStatus() != AsyncTask.Status.FINISHED) {
                        task.cancel(true);
                        task.isCancelled();
                    }
                }
            }
        });
    }

    public boolean isOnline()
    {
        ConnectivityManager cm= (ConnectivityManager) getBaseContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if(netInfo != null && netInfo.isConnected())
        {
            return true;
        }

        return false;
    }
}
